package com.cn.bukeManage.service.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.cn.bukeManage.dao.PurchaseDao;
import com.cn.bukeManage.pojo.Composition;
import com.cn.bukeManage.pojo.Supplier;
import com.cn.bukeManage.service.PurchaseService;
import com.cn.bukeManage.utils.MyDate;
import com.cn.bukeManage.utils.ClothCurtain.Calculate;



@Service("purchaseService")
public class PurchaseServiceImpl implements PurchaseService {
	@Resource
	private PurchaseDao purchaseDao;

	//获取所有需要采购的原材料
	public List<Map<String, Object>> getAllPurchase(Model model) {
		//得到当天需要采购的所有商品 订单状态是3,制作中, 前一天五点之后, 今天五点之前最后审核时间
		Map<String,Object> map=new HashMap<>();
		map.put("time1",MyDate.getTimeForMe(0));
		map.put("time2",MyDate.getTimeForMe(1));
		// goods= orderId,g.sampleId,g.goodsId,s.type
		List<Map<String, Object>> goods = purchaseDao.getAllGoodsByOrderStatusAndDate(map);
		//得到这个商品类型, 如果这个商品是布帘, 那么就看看这个商品里面是不是有配件
		//一个装布帘所有配件的集合
		List<Map<String,Object>> accList=new ArrayList<>();
		for(Map<String,Object> map2:goods){
			Integer type=Integer.parseInt(map2.get("type").toString());
			if(type==0){
				//得到布帘的所有配件
				List<Map<String,Object>> goodsDeatils= purchaseDao.getAccOfGoodsByGoodsDeatail(Integer.parseInt(map2.get("goodsId").toString()));
				if(goodsDeatils.size()>0){
					accList.addAll(goodsDeatils);
				}
			}
		}
		goods.addAll(accList);
		//整理一下,统计所有用量
		//布帘构成属性存储
		List<Map<String,Object>> bulian=new ArrayList<>();
		//成品帘量属性存储
		List<Map<String,Object>> chengpinLian=new ArrayList<>();
		//配件属性存储
		List<Map<String,Object>> accAttr=new ArrayList<>();
		for(Map<String,Object> map2:goods){
			Integer type=Integer.parseInt(map2.get("type").toString());
			if(type==0){
					//根据商品id获取所有商品构成
					List<Map<String,Object>> goucheng= purchaseDao.getGouChengAttrByGoodsId(Integer.parseInt(map2.get("goodsId").toString()));
					
					List<Composition> zhupeibu=new ArrayList<>();
					Integer orderId=(Integer)goucheng.get(0).get("orderId");
					//纱的用量
					List<Map<String,Object>> shaList=new ArrayList<>();
					for(Map<String,Object> map3:goucheng){
						//把商品构成
						Map<String,Object> map4=new HashMap<>();
						//订单id
						map4.put("orderId",orderId);
						if(type==1){
							//主布
							Composition composition=new Composition();
							composition.setPutNumber((Double)map3.get("putNumber"));
							composition.setPutTogether((Integer)map3.get("putTogether"));
							composition.setSerialnumber((String)map3.get("serialnumber"));
							composition.setBreadth((Double)map3.get("breadth"));
							composition.setBreadthStatus((Integer)map3.get("breadthStatus"));
							composition.setSpur((Double)map3.get("spur"));
							composition.setType((Integer)map3.get("type"));
							composition.setInverte((Integer)map3.get("inverte"));
							zhupeibu.add(composition);
						}else if(type==2){
							//配布
							Composition composition=new Composition();
							composition.setPutNumber((Double)map3.get("putNumber"));
							composition.setPutTogether((Integer)map3.get("putTogether"));
							composition.setSerialnumber((String)map3.get("serialnumber"));
							composition.setBreadth((Double)map3.get("breadth"));
							composition.setBreadthStatus((Integer)map3.get("breadthStatus"));
							composition.setSpur((Double)map3.get("spur"));
							composition.setType((Integer)map3.get("type"));
							composition.setInverte((Integer)map3.get("inverte"));
							zhupeibu.add(composition);							
						}else if(type==3){
							//纱
							//一系列参数赋值
							Double breadth=(Double)map3.get("breadth");
							Double times_fold=(Double)map3.get("times_fold");
							Double spur=(Double)map3.get("spur");
							Double hem=(Double)map3.get("hem");
							Integer inverte=(Integer)map3.get("inverte");
							Double wide=(Double)map3.get("yarnWide");
							Double high=(Double)map3.get("yarnHigh");
							Integer breadthStatus=(Integer)map3.get("breadthStatus");							
							Map<String,Object> map5=Calculate.FixedNPin(breadth, times_fold, spur, hem, inverte, wide, high, breadthStatus);
							Supplier supplier= purchaseDao.getSupplierBySerialnumber((String)map5.get("serialnumber"));
							map4.put("name",supplier.getName());
							map4.put("address",supplier.getAddress());	
							map4.putAll(map5);
							
						}else if(type==4){
							//遮光里布
							//一系列参数赋值
							Double breadth=(Double)map3.get("breadth");
							Double times_fold=(Double)map3.get("times_fold");
							Double spur=(Double)map3.get("spur");
							Double hem=(Double)map3.get("hem");
							Integer inverte=(Integer)map3.get("inverte");
							Double wide=(Double)map3.get("clothWide");
							Double high=(Double)map3.get("clothHigh");
							Integer breadthStatus=(Integer)map3.get("breadthStatus");							
							Map<String,Object> map5=Calculate.FixedNPin(breadth, times_fold, spur, hem, inverte, wide, high, breadthStatus);
							Supplier supplier= purchaseDao.getSupplierBySerialnumber((String)map5.get("serialnumber"));
							map4.put("name",supplier.getName());
							map4.put("address",supplier.getAddress());	
							map4.putAll(map5);						
						}else if(type==5){
							//半遮光里布
							//一系列参数赋值
							Double breadth=(Double)map3.get("breadth");
							Double times_fold=(Double)map3.get("times_fold");
							Double spur=(Double)map3.get("spur");
							Double hem=(Double)map3.get("hem");
							Integer inverte=(Integer)map3.get("inverte");
							Double wide=(Double)map3.get("clothWide");
							Double high=(Double)map3.get("clothHigh");
							Integer breadthStatus=(Integer)map3.get("breadthStatus");							
							Map<String,Object> map5=Calculate.FixedNPin(breadth, times_fold, spur, hem, inverte, wide, high, breadthStatus);
							Supplier supplier= purchaseDao.getSupplierBySerialnumber((String)map5.get("serialnumber"));
							map4.put("name",supplier.getName());
							map4.put("address",supplier.getAddress());	
							map4.putAll(map5);
						}
						
						shaList.add(map4);
					}
					
					Double times_fold=(Double)goucheng.get(0).get("times_fold");
					Double hem=(Double)goucheng.get(0).get("hem");
					Integer production=(Integer)goucheng.get(0).get("production");
					Double wide=(Double)goucheng.get(0).get("clothWide");
					Double high=(Double)goucheng.get(0).get("clothHigh");						
					Map<String, Object> zhuPeiBuYongLiang = Calculate.FixedWidePin(times_fold, hem, wide, high, production, zhupeibu);
					if (zhuPeiBuYongLiang.get("peibuList")!=null) {
						@SuppressWarnings("unchecked")
						List<Map<String,Object>> maps=(List<Map<String,Object>>)zhuPeiBuYongLiang.get("peibuList");
						for(Map<String,Object> map3:maps){
							Supplier supplier = purchaseDao.getSupplierBySerialnumber((String)map3.get("serialnumber"));
							map3.put("name",supplier.getName());
							map3.put("address",supplier.getAddress());
							map3.put("orderId",orderId);
						}
						//添加到最外层
						bulian.addAll(maps);
					}
					if (zhuPeiBuYongLiang.get("zhubuList")!=null) {
						@SuppressWarnings("unchecked")
						List<Map<String,Object>> maps=(List<Map<String,Object>>)zhuPeiBuYongLiang.get("zhubuList");
						for(Map<String,Object> map3:maps){
							Supplier supplier = purchaseDao.getSupplierBySerialnumber((String)map3.get("serialnumber"));
							map3.put("name",supplier.getName());
							map3.put("address",supplier.getAddress());
							map3.put("orderId",orderId);
						}
						//添加到最外层
						bulian.addAll(maps);
					}
					bulian.addAll(shaList);
			}else if(type==1){
				//成品帘 clothHigh   clothWide
				Map<String,Object> map3=purchaseDao.getFinishShadeByGoodId(Integer.parseInt(map2.get("goodsId").toString()));
				//把这些信息存到List中
				chengpinLian.add(map3);
			}else if(type==2){
				//配件
				Map<String,Object> map3=purchaseDao.getAccByGoodId(Integer.parseInt(map2.get("goodsId").toString()));
				accAttr.add(map3);
			}
		}	
		//整理布帘, 算算布帘一个商品可以 
		List<Map<String,Object>> zong=new ArrayList<>();
		zong.addAll(bulian);
		zong.addAll(chengpinLian);
		zong.addAll(accAttr);
		//TODO 根据供货商的不同, 给他们分类, 整体一个订单也是要分类的  ,同一个型号的布可能一起减, 然后采购的数量要减去库存里面的, 剩余的要存库存
		//map中 :有订单号, 供货商号    
		//map里面包含的值: 
		//成品帘:f.fptBody,g.clothHigh,g.clothWide,g.orderId,s.type,s.sampleId,s.supplierId,s.structure,s.serialnumber ,su.name,su.address
		//配件:g.addpeijian,g.orderId,g.number,g.saleNumber,g.acc,s.supplierId,su.address,su.name ,ac.accessoriesTypeContent
		//布帘:name ,address,orderId, dosage, clothNumber, useWide, serialnumber, actualHigh, useInverte, actualWide, useHigh
		
		
		
		return zong;
	}



}
